// pages/add-share/index.js
var api = require('../../api.js');
let time = require('../../utils/utils.js');
var apple = require('../../app.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgServer: api.default.img_server,
    form: {
      nickname: '',
      gender: 0,
      phone: '',
      job: '',
      birthday: Date,
      city: '',
    },
    img: "/images/img-share-un.png",
    agree: 0,
    birthdayisEmpty: true,
    attr_group_list:[{id:0,name:200,value:5,real:195,checked:true},{id:1,name:600,value:25,real:575,checked:false},{id:2,name:1000,value:50,real:950,checked:false}],
    attr_group_list2:[{id:0,name:600,value:10,real:590,checked:true},{id:1,name:800,value:20,real:780,checked:false},{id:2,name:1000,value:30,real:970,checked:false}],
    attr_group_list3:[{id:0,name:2000,value:10,real:1990,checked:true}],
    realnumb:195
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_info = wx.getStorageSync("my_info");
    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code;
          getApp().loginrequest({
            url: api.passport.quick_login,
            method: "post",
            data: {
              code: code
            },
            success: function (res) {
              console.log("quickLoginSuccessful")
              console.log(res)
              wx.hideLoading();
              if (res.code == 200) {
                const tokenStr = res.data.tokenHead + res.data.token
                wx.setStorageSync('loginToken', tokenStr)
                wx.setStorageSync('token', res.data.token);
                wx.setStorageSync("access_token", res.data.userId);
                wx.setStorageSync("user_info", {
                  avatar_url: res.data.img,
                  nickname: res.data.username,
                  avatar_url: res.data.icon,
                  // parent: res.data.parent,
                  id: res.data.id
                });
                if(res.data.myInfo.memberLevelId ==1){
                  page.data.attr_group_list =page.data.attr_group_list3;
                  page.data.realnumb = page.data.attr_group_list3[0].real;
                  page.setData({
                    attr_group_list:page.data.attr_group_list3,
                    realnumb:page.data.attr_group_list3[0].real
                  });
                }else if(res.data.myInfo.memberLevelId ==2){
                  page.data.attr_group_list =page.data.attr_group_list2;
                  page.data.realnumb = page.data.attr_group_list2[0].real;
                  page.setData({
                    attr_group_list:page.data.attr_group_list2,
                    realnumb:page.data.attr_group_list2[0].real
                  });  
                  }
                wx.setStorageSync("my_info", {
                  supplyId: res.data.myInfo.supplyId,
                  supplyState: res.data.myInfo.supplyState,
                  myMoney: res.data.myInfo.myMoney,
                  icon: res.data.myInfo.icon,
                  nickname: res.data.myInfo.nickname,
                  memberLevelId: res.data.myInfo.memberLevelId,
                  id: res.data.myInfo.id,
                  city: res.data.myInfo.city,
                  gender: res.data.myInfo.gender,
                  job: res.data.myInfo.job,
                  phone: res.data.myInfo.phone,
                  birthday: res.data.myInfo.birthday,
                  brokerage: res.data.myInfo.brokerage,
                  brokerageTixian: res.data.myInfo.brokerageTixian,

                  moneyTotal: res.data.myInfo.moneyTotal,
                  buyTotal: res.data.myInfo.buyTotal,

                  // parent: res.data.parent,
                });
                page.setData({
                  store: wx.getStorageSync('store'),
                  myInfo: wx.getStorageSync('my_info')
                });
              }
            }
          });
        }


      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var store = wx.getStorageSync("store");
    var share_setting = wx.getStorageSync("share_setting");
    page.setData({
      user_info: user_info,
      store: store,
      share_setting: share_setting
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  formSubmit: function (e) {
    var page = this;
    wx.requestSubscribeMessage({
      tmplIds: ['qZOpyTxHMMB9EibgkUtm5mv9_lkQ09PYvwzdw6-pzqw','sQatSUucQ6Sl6Kh0aHZSPd8a9MgNVlO6SrHAwWHUvGs'],
      success(res) {
          page.formSubmit2(e);
        console.log(res)
      }
    })
},

  formSubmit2: function (e) {
    var page = this;
    var user_info = wx.getStorageSync("user_info");
    var my_info = wx.getStorageSync("my_info");
    console.log(e.detail);
    page.data.form = e.detail.value;
    //获取支付数据
    app.request({
      url: api.order.chongzhi,
      data: {
        money: page.data.realnumb,
        payType: 2,
      },
      success: function (res) {
        console.log(res);
       
     
        if (res.code == 200) {
          //发起支付
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function (e) {
              console.log('1');
              console.log(e);
              wx.requestSubscribeMessage({
                tmplIds: ['bPgXd4I-7E-GYafPJSNJEAQMuptkU_SYG1OscN7MM0o'],
                success(res) {
                  console.log(res)
                },
                fail(error) {
                  console.log(error)
                }
              });
            },
            fail: function (e) {
              console.log('2');
              console.log(e);
            },
            complete: function (e) {
              console.log('3');
              console.log(e);
              if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {//支付失败转到待支付订单列表
                wx.showModal({
                  title: "提示",
                  content: "充值失败",
                  showCancel: false,
                  confirmText: "确认",
                  success: function (res) {
                    if (res.confirm) {
                      wx.redirectTo({
                        url: "/pages/user/user?status=9",
                      });
                    }
                  }
                });
                return;
              }
              if (e.errMsg == "requestPayment:ok") {
                wx.showModal({
                  title: "提示",
                  content: "充值成功",
                  showCancel: false,
                  confirmText: "确认",
                  success: function (res) {
                    wx.redirectTo({
                      url: "/pages/user/user?status=9",
                    });
                  }
                });
                return;
              }
              wx.redirectTo({
                url: "/pages/user/user?status=1",
              });
            },
          });
          return;
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
          });
          return;
        }
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  attrClick: function (e) {

    var page = this;
    var attr_id = e.currentTarget.dataset.id; 
    var attr_group_list = page.data.attr_group_list;
  console.log(attr_group_list)
  var ss = 0
  var dd = 0

    for (var i in attr_group_list) {
        attr_group_list[i].checked = false;
        if (attr_group_list[i].id == attr_id){
            attr_group_list[i].checked = true;
            ss = attr_group_list[i].real;
            dd=attr_group_list[i].value
        }
    }
    page.setData({
        attr_group_list: attr_group_list,
        realnumb: ss,
    });


},





  isBlank: function (text) {
    console.log(text)
    if (text == undefined || text == '') {
      wx.showModal({
        content: "请填写充值金额!",
        showCancel: false,
      })
      return true
    }
    return false
  }
})